package lion.framework.core.db.paging;

import lion.framework.core.db.anno.SQLPaging;

/**
 * Oracle 数据库分页 行数从1开始记
 * 
 * <pre>
 * time   2013 2013-4-24 上午11:28:50 
 * mail   hl_0074@sina.com
 * </pre>
 * 
 * @author lion
 */
@SQLPaging("oracle")
public class OracleSQLPaging implements ISQLPaging {

	@Override
	public String paging(String sql, int startrowNum, int pageSize) {

		if (pageSize < 0) { return sql; }
		if (startrowNum < 1) {
			startrowNum = 1;
		}
		int endRow = startrowNum;

		if (pageSize > 0) {
			endRow += pageSize;
		}

		StringBuilder buf = new StringBuilder();
		buf.append("select * from (select tmp_.*,rownum rn_ from(" + sql + ") tmp_) where rn_>=" + startrowNum);

		if (endRow > 1) {
			buf.append(" and rn_<" + endRow);
		}

		return buf.toString();
	}

}
